package com.itheima.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.itheima.core.BaseServlet;
import com.itheima.domain.system.Dept;
import com.itheima.service.system.DeptService;
import com.itheima.service.system.impl.DeptServiceImpl;
import com.itheima.utils.BeanUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/system/dept/*")
public class DeptServlet extends BaseServlet {
    private DeptService deptService = new DeptServiceImpl();

    /**
     * 查询部门列表
     */
    public Object list(HttpServletRequest request, HttpServletResponse response)throws Exception{
        /*1.获取表单数据*/
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");

        if(StringUtil.isEmpty(pageNum)){
            pageNum = "1";
        }
        if(StringUtil.isEmpty(pageSize)){
            pageSize = "10";
        }

        /*2.调用service*/
        PageInfo<Dept> pageInfo = deptService.pageQuery(Integer.parseInt(pageNum),Integer.parseInt(pageSize));

        /*3.页面跳转*/
        request.setAttribute("page",pageInfo);
        return "/WEB-INF/pages/system/dept/list.jsp";

    }

    /**
     * 访问新建部门页面
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public Object toAdd(HttpServletRequest request, HttpServletResponse response)throws Exception{
        List<Dept> depts = deptService.findAll();

        request.setAttribute("deptList",depts);
        return "/WEB-INF/pages/system/dept/add.jsp";
    }

    /**
     * 新增部门
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public Object save(HttpServletRequest request, HttpServletResponse response)throws Exception{

        Dept dept = BeanUtil.fillBean(request, Dept.class);

        deptService.save(dept);


        return "/system/dept/list";
    }


    /**
     * 访问编辑页面
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public Object toEdit(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String id = request.getParameter("id");

        Dept dept = deptService.findById(id);
        List<Dept> depts = deptService.findAll();

        request.setAttribute("dept",dept);
        request.setAttribute("deptList",depts);
        return "/WEB-INF/pages/system/dept/update.jsp";
    }

    /**
     * 编辑部门
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public Object edit(HttpServletRequest request, HttpServletResponse response)throws Exception{
        Dept dept = BeanUtil.fillBean(request, Dept.class);

        deptService.update(dept);

        return "/system/dept/list";
    }

    /**
     * 删除部门
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public Object delete(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String[] ids = request.getParameterValues("id");

        deptService.delete(ids);

        return "/system/dept/list";
    }
}
